package ssn.task.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import ssn.common.util.FileUtil;
import ssn.common.util.MD5Hash;
import ssn.servicearea.service.ServiceAreaService;
import ssn.task.service.TaskService;
import ssn.task.vo.TaskVO;
import ssn.user.vo.UserVO;
import egovframework.rte.fdl.cmmn.AbstractServiceImpl;
import egovframework.rte.fdl.property.EgovPropertyService;
import egovframework.rte.fdl.string.EgovStringUtil;

/**  
 * @Class Name : 
 * @Description : 
 * 
 * @author ksh
 * @since 2013. 09.26
 * @version 1.0
 * @see
 */
@SuppressWarnings({"rawtypes","unchecked"})
@Service("taskService")
public class TaskServiceImpl extends AbstractServiceImpl implements TaskService {
	
	@Resource(name="taskDAO")
	private TaskDAO taskDAO; 
	
	@Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
	
	@Resource(name = "serviceareaService")
    private ServiceAreaService serviceareaService;
	
	/**
	 * 업무연락 정보 입력
	 */
	public void insertTaskInfo(TaskVO vo) throws Exception{
		if(EgovStringUtil.isNotEmpty(vo.getSto_task_file())){
			vo.setSto_task_file(this.taskFilemove(vo));
		}
		vo.setSto_task_key(taskDAO.selectTaskInfoPk());
		
		taskDAO.insertTaskInfo(vo);
		taskDAO.insertTaskServiceInfo(vo);
	}
	
	/**
	 * 업무연락 정보 수정
	 */
	public void updateTaskInfo(TaskVO vo) throws Exception{
		String rootPath = propertiesService.getString("file.root.path");
		if(vo.getSto_task_file().indexOf("/") == -1 && EgovStringUtil.isNotEmpty(vo.getSto_task_file())){	
			String filePath = taskDAO.selectTaskAttachFile(vo.getSto_task_key());
			if(EgovStringUtil.isNotEmpty(filePath)){
				FileUtil.deleteFile(rootPath+getAttachFileSplit(filePath)[0]);
			}
			vo.setSto_task_file(this.taskFilemove(vo));
		}
		taskDAO.updateTaskInfo(vo);
		taskDAO.deleteTaskService(vo.getSto_task_key());
		taskDAO.insertTaskServiceInfo(vo);
	}
	
	/**
	 * 삭제
	 * @param vo
	 * @throws Exception
	 */
	public void updateStatusTaskInfo(TaskVO vo) throws Exception{
		taskDAO.updateStatusTaskInfo(vo);
	}
	
	/**
	 * 업무연락 정보 삭제(DB 삭제) 사용안함.
	 */
	public void deleteTaskInfo(TaskVO vo) throws Exception{
		taskDAO.deleteTaskInfo(vo);
	}
	
	/**
	 * 업무연락 정보 리스트 및 전체 카운트
	 */
	public Map selectTaskList(TaskVO vo) throws Exception{
		Map resultMap = new HashMap();
		resultMap.put("resultList", taskDAO.selectTaskList(vo));
		resultMap.put("resultListCnt", taskDAO.selectTaskListCnt(vo));
		return resultMap;
	}
	/**
	 * 업무연락 정보 관리자 리스트 및 전체 카운트
	 */
	public Map selectTaskListAdmin(TaskVO vo) throws Exception{
		Map resultMap = new HashMap();
		resultMap.put("resultList", taskDAO.selectTaskListAdmin(vo));
		resultMap.put("resultListCnt", taskDAO.selectTaskListAdminCnt(vo));
		return resultMap;
	}
	/**
	 * 업무연락 정보 조회
	 */
	public TaskVO selectTaskInfo(TaskVO vo, String sts_area_key) throws Exception{
		vo.setSto_area_key(sts_area_key);
		taskDAO.updateTaskServiceReadTime(vo);
		return taskDAO.selectTaskInfo(vo);
	}
	/**
	 * 업무연락 정보 조회 관리자
	 */
	public TaskVO selectTaskInfoAdmin(TaskVO vo) throws Exception{
		return taskDAO.selectTaskInfoAdmin(vo);
	}
	
	/**
	 * 업무연락 기타 정보
	 */
	public Map selectTaskOtherInfo() throws Exception{
		Map resultMap = new HashMap();
		resultMap.put("serviceareaList", serviceareaService.selectServiceAreaList());
		return resultMap;
	}
	
	/**
	 * 업무연락 첨부파일 이동
	 */
	public String taskFilemove(TaskVO vo){
		String realPath = propertiesService.getString("file.real.path");
		String tempPath = propertiesService.getString("file.temp.path");
		String[] fileInfo = getAttachFileSplit(vo.getSto_task_file());
		return FileUtil.moveFiles(tempPath, realPath, fileInfo[0])+"**"+fileInfo[1]+"**"+fileInfo[2];
	}
	
	/**
	 * 첨부파일 삭제
	 */
	public boolean deleteTaskAttachFile(String sto_task_key) {
		String rootPath = propertiesService.getString("file.root.path");
		String attachFilePath = "";
		try {
			attachFilePath = taskDAO.selectTaskAttachFile(sto_task_key);
			FileUtil.deleteFile(rootPath+getAttachFileSplit(attachFilePath)[0]);
			taskDAO.deleteTaskAttachFile(sto_task_key);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	/**
	 * 첨부파일 자르기
	 */
	public String[] getAttachFileSplit(String attachFile){
		String[] result = new String[3];
		String[] temp = attachFile.split("\\*\\*");
		for(int i=0; i<result.length;i++){
			result[i] = temp[i]==null?"":temp[i];
		}
		return result;
	}
}
